<template>
  <div v-loading="loading" class="departments-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- 缺少treeNode -->
        <tree-tools :tree-node="company" is-root @addDepts="addDepts" />
        <!--放置一个属性   这里的props和我们之前学习的父传子 的props没关系-->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
          <!-- scope-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
          <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
          <!-- data表示当前子节点的数据 -->
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartment"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 弹窗 -->
    <add-depts
      ref="addDepts"
      :visible.sync="dialogVisible"
      :tree-node="node"
      @addDepts="getDepartment"
    />
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import addDepts from './components/add-depts.vue'
import { getDepartmentList } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  components: { treeTools, addDepts },
  data() {
    return {
      company: {}, // 头部数据结构
      defaultProps: {
        label: 'name' // 从这里显示内容
      },
      departs: [],
      dialogVisible: false, // 控制弹窗关闭
      node: {}, // 当前节点信息
      loading: false
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 编辑
    editDepts(treeNode) {
      this.dialogVisible = true
      this.node = treeNode
      this.$refs.addDepts.getDepartmentDetail(treeNode.id)
    },
    // 新增
    addDepts(treeNode) {
      this.dialogVisible = true
      this.node = treeNode
    },
    // 获取部门列表数据
    async getDepartment() {
      this.loading = true
      const { depts, companyName } = await getDepartmentList()
      this.departs = tranListToTreeData(depts, '')
      this.company = { name: companyName, manager: '负责人', id: '' }
      this.loading = false
    }
  }

}

</script>

<style>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
